﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebCarERP.Models;

namespace WebCarERP.Controllers
{
    public class LoginController : Controller
    {
        //
        // GET: /Login/
        protected static WebCarERPEntities db = new WebCarERPEntities();
        //
        // GET: /Login/
        //TestConfigCache为在配置文件中配置的缓存节
        /// <summary>
        /// 登录页初始化
        /// </summary>
        /// <returns></returns>
        [OutputCache(CacheProfile = "TestConfigCache")]
        public ActionResult Index()
        {
            if (Session["RoleUserModel"] != null)
            {
                Response.Redirect("/Home/Index");
            }
            //获取到Cookie中的值传递给前台显示
            var Phone = Request.Cookies["Phone"] == null ? "" : Request.Cookies["Phone"].Value;
            var Pwd = Request.Cookies["Pwd"] == null ? null : Request.Cookies["Pwd"].Value;
            ViewBag.Phone = Phone;
            ViewBag.Pwd = Pwd;
            ViewBag.jizhu = Request.Cookies["jizhu"] == null ? null : Request.Cookies["jizhu"].Value;
            //ViewBag.Time = DateTime.Now;
            return View();
        }

        /// <summary>
        /// 登录信息验证
        /// </summary>
        /// <param name="tb_user">用户信息</param>
        /// <param name="Code">验证码</param>
        /// <returns></returns>

        [HttpPost]
        // [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult CheckUserLogin(UserInfo user, bool jizhu = false)
        {
            var Phone = Server.UrlDecode(user.Phone);// user.bianma;
            var Pwd = Server.UrlDecode(user.Pwd);
            //如果有用户名的话讲用户名存放到Cookie中
            if (Phone != null)
            {
                Response.Cookies["Phone"].Value = Phone;
                Response.Cookies["Phone"].Expires = DateTime.Now.AddDays(7);
            }

            ////这里验证用户输入的验证码和系统的验证码是否相同
            //string sessionCode = Session["ValidateCode"] == null ? new Guid().ToString() : Session["ValidateCode"].ToString();

            ////将验证码去掉，避免暴力破解
            //Session["ValidateCode"] = new Guid();

            //if (sessionCode != Code)
            //{
            //    return Content("请输入正确的验证码");
            //}
 
            //使用MD5加密
            //string password_md5 = Md5.Md5Hash(Md5.Md5Hash(Pwd.Trim().ToUpper()));
            //  调用BLL检验用户名密码是否正确
            RoleUserModel model = (from x in db.UserInfo.AsNoTracking().Where(t => (t.Phone == Phone || t.Mail == Phone) && t.DelFlag == 1 && t.Pwd == Pwd)
                                 
                                   select new RoleUserModel
                                   {
                                       ID = x.ID,
                                       UName = x.UName,
                                       Pwd = x.Pwd,
                                       Phone = x.Phone,
                                       Mail = x.Mail,
                                       SubTime = x.SubTime,
                                       LastModifiedOn = x.LastModifiedOn,
                                       DelFlag = x.DelFlag,
                                   }).FirstOrDefault();

            //.ToList().FirstOrDefault();
            if (model != null)
            {
            
                //提供Session接口方便后面判断用户登录
                Session["RoleUserModel"] = model;
                HttpContext.Cache.Insert(model.ID.ToString(), HttpContext.Session.SessionID, null, DateTime.Now.AddMinutes(180), TimeSpan.Zero);

                string ip = Common.Common.GetIP();
                tb_login tbLogin = new tb_login() { memberid = model.ID, name = model.UName, ip = ip, logintime = DateTime.Now };
                db.tb_login.Add(tbLogin);
                int j = db.SaveChanges();
                if (jizhu)
                {
                    
                        Response.Cookies["Pwd"].Value = Pwd;
                        Response.Cookies["Pwd"].Expires = DateTime.Now.AddDays(7);
                        Response.Cookies["jizhu"].Value = "checked";
                        Response.Cookies["jizhu"].Expires = DateTime.Now.AddDays(7);
               
                }
                else
                {
                    Response.Cookies["Pwd"].Value = null;
                    Response.Cookies["Pwd"].Expires = DateTime.Now.AddDays(-1);
                    Response.Cookies["jizhu"].Value = null;
                    Response.Cookies["jizhu"].Expires = DateTime.Now.AddDays(-1);
                }
                //if (Pwd.Equals("123456") )
                //{
                //    return Content("Reset");
                //}
                return Content("OK");
            }
            else
            {
                Response.Cookies["Pwd"].Value = null;
                Response.Cookies["Pwd"].Expires = DateTime.Now.AddDays(-1);
                //>用户名或用户密码错误或您的会员身份被锁定，请重新输入！
                return Content("<font color=#FF0000><b>用户名或用户密码错误！</b></font>");
            }
        }

    }
}
